-module(logfile).

-include_lib("eunit/include/eunit.hrl").

-define(FILENAME, "test/examples/" ?MODULE_STRING ".yaml").

setup() ->
    application:start(yamerl).

simple_test_() ->
    {setup,
      fun setup/0,
      [
        ?_assertMatch(
          [
            [
              {"Time","2001-11-23 15:01:42 -5"},
              {"User","ed"},
              {"Warning","This is an error message for the log file"}],
            [
              {"Time","2001-11-23 15:02:31 -5"},
              {"User","ed"},
              {"Warning","A slightly different error message."}],
            [
              {"Date","2001-11-23 15:03:17 -5"},
              {"User","ed"},
              {"Fatal","Unknown variable \"bar\""},
              {"Stack",
                [
                  [
                    {"file","TopClass.py"},
                    {"line",23},
                    {"code","x = MoreObject(\"345\\n\")\n"}],
                  [
                    {"file","MoreClass.py"},
                    {"line",58},
                    {"code","foo = bar"}]]}]],
          yamerl_constr:file(?FILENAME, [{detailed_constr, false}])
        )
      ]
    }.

detailed_test_() ->
    {setup,
      fun setup/0,
      [
        ?_assertMatch(
          [
            {yamerl_doc,{yamerl_map,yamerl_node_map,
                "tag:yaml.org,2002:map",
                [{line,2},{column,1}],
                [
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,2},{column,1}],
                      "Time"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,2},{column,7}],
                      "2001-11-23 15:01:42 -5"}},
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,3},{column,1}],
                      "User"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,3},{column,7}],
                      "ed"}},
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,4},{column,1}],
                      "Warning"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,5},{column,3}],
                      "This is an error message for the log file"}}]}},
            {yamerl_doc,{yamerl_map,yamerl_node_map,
                "tag:yaml.org,2002:map",
                [{line,8},{column,1}],
                [
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,8},{column,1}],
                      "Time"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,8},{column,7}],
                      "2001-11-23 15:02:31 -5"}},
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,9},{column,1}],
                      "User"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,9},{column,7}],
                      "ed"}},
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,10},{column,1}],
                      "Warning"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,11},{column,3}],
                      "A slightly different error message."}}]}},
            {yamerl_doc,{yamerl_map,yamerl_node_map,
                "tag:yaml.org,2002:map",
                [{line,14},{column,1}],
                [
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,14},{column,1}],
                      "Date"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,14},{column,7}],
                      "2001-11-23 15:03:17 -5"}},
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,15},{column,1}],
                      "User"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,15},{column,7}],
                      "ed"}},
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,16},{column,1}],
                      "Fatal"},
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,17},{column,3}],
                      "Unknown variable \"bar\""}},
                  {
                    {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                      [{line,18},{column,1}],
                      "Stack"},
                    {yamerl_seq,yamerl_node_seq,"tag:yaml.org,2002:seq",
                      [{line,19},{column,3}],
                      [
                        {yamerl_map,yamerl_node_map,"tag:yaml.org,2002:map",
                          [{line,19},{column,5}],
                          [
                            {
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,19},{column,5}],
                                "file"},
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,19},{column,11}],
                                "TopClass.py"}},
                            {
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,20},{column,5}],
                                "line"},
                              {yamerl_int,yamerl_node_int,"tag:yaml.org,2002:int",
                                [{line,20},{column,11}],
                                23}},
                            {
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,21},{column,5}],
                                "code"},
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,21},{column,11}],
                                "x = MoreObject(\"345\\n\")\n"}}]},
                        {yamerl_map,yamerl_node_map,"tag:yaml.org,2002:map",
                          [{line,23},{column,5}],
                          [
                            {
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,23},{column,5}],
                                "file"},
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,23},{column,11}],
                                "MoreClass.py"}},
                            {
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,24},{column,5}],
                                "line"},
                              {yamerl_int,yamerl_node_int,"tag:yaml.org,2002:int",
                                [{line,24},{column,11}],
                                58}},
                            {
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,25},{column,5}],
                                "code"},
                              {yamerl_str,yamerl_node_str,"tag:yaml.org,2002:str",
                                [{line,25},{column,11}],
                                "foo = bar"}}]}],
                      2}}]}}
          ],
          yamerl_constr:file(?FILENAME, [{detailed_constr, true}])
        )
      ]
    }.
